package com.gc.web.controller.business;

import java.util.List;
import java.util.Arrays;

import cn.hutool.core.date.DateUtil;
import com.gc.common.base.Result;
import com.gc.common.support.Convert;
import com.gc.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gc.common.annotation.Log;
import com.gc.common.enums.BusinessType;
import com.gc.business.domain.RegisterAgreement;
import com.gc.business.service.IRegisterAgreementService;
import com.gc.framework.web.base.BaseController;
import com.gc.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 注册协议 控制层
 * 
 * @author liujinteng
 * @date 2020-12-29
 */
@Slf4j
@Controller
@RequestMapping("/business/registerAgreement")
public class RegisterAgreementController extends BaseController {
    private String prefix = "business/registerAgreement";
	
	private final IRegisterAgreementService registerAgreementService;

	@Autowired
	public RegisterAgreementController(IRegisterAgreementService registerAgreementService) {
		this.registerAgreementService = registerAgreementService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:registerAgreement:view")
	@GetMapping
	public String registerAgreement(){
	    return prefix + "/registerAgreement";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{registerId}")
	public String edit(@PathVariable("registerId") Long registerId, ModelMap mmap){
		RegisterAgreement registerAgreement = registerAgreementService.getById(registerId);
		mmap.put("registerAgreement", registerAgreement);
		return prefix + "/edit";
	}
	
	/**
	 * 查询注册协议列表
	 */
	@RequiresPermissions("business:registerAgreement:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(RegisterAgreement registerAgreement){
		startPage();
        List<RegisterAgreement> list = registerAgreementService.list(registerAgreement);
		return Result.success(getDataTable(list));
	}

	/**
	 * 新增保存注册协议
	 */
	@RequiresPermissions("business:registerAgreement:add")
	@Log(title = "注册协议", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(RegisterAgreement registerAgreement){
		registerAgreementService.save(registerAgreement);
		return Result.success();
	}

	/**
	 * 修改保存注册协议
	 */
	@RequiresPermissions("business:registerAgreement:edit")
	@Log(title = "注册协议", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(RegisterAgreement registerAgreement){
		registerAgreement.setUpdateBy(ShiroUtils.getLoginName());
		registerAgreement.setUpdateTime(DateUtil.date());
		registerAgreementService.updateById(registerAgreement);
		return Result.success();
	}
	
	/**
	 * 删除注册协议
	 */
	@RequiresPermissions("business:registerAgreement:remove")
	@Log(title = "注册协议", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		registerAgreementService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出注册协议列表
	 */
	@RequiresPermissions("business:registerAgreement:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(RegisterAgreement registerAgreement){
		List<RegisterAgreement> list = registerAgreementService.list(registerAgreement);
		ExcelUtil<RegisterAgreement> util = new ExcelUtil<>(RegisterAgreement.class);
		util.exportExcel(list, "registerAgreement");
		return Result.success();
	}
}
